
package com.kugou.game.sdk.ui.dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.kugou.game.sdk.core.CouponHelpModel;
import com.kugou.game.sdk.entity.CouponHelpInfo;
import com.kugou.game.sdk.interfaces.OnRequestListener;
import com.kugou.game.sdk.ui.widget.LoadingView;
import com.kugou.game.sdk.utils.Rx;

/**
 * 描述:代金券帮助对话框
 *
 * @author wcj
 * @since 2016年5月20日 下午3:57:13
 */
public class CouponHelpDialog extends Dialog {

    private final static int MSG_REQUEST_DATA_SUCCESS = 1;

    private final static int MSG_REQUEST_DATA_FAILED = 2;

    private LayoutInflater mInflater;

    private ListView mListView;

    private LoadingView mLoadingView;

    private Activity mContext;

    private CouponHelpAdapter mAdapter;

    private ArrayList<CouponHelpInfo> mOptions = new ArrayList<CouponHelpInfo>();

    private Handler mHandler;

    public CouponHelpDialog(Activity context, int theme) {
        super(context, theme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        this.mContext = context;
        initView();
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        setDialogDefaultSize();

        if (CouponHelpModel.getCouponHelpModel().getHelpInfos() != null) {
            mOptions = CouponHelpModel.getCouponHelpModel().getHelpInfos();
            mAdapter.notifyDataSetChanged();
        } else {
            mLoadingView.setVisibility(View.VISIBLE);
            mLoadingView.setText("加载数据中");
            CouponHelpModel.getCouponHelpModel().requestHelpInfos(new OnRequestListener() {

                @Override
                public void onSuccess() {
                    mOptions = CouponHelpModel.getCouponHelpModel().getHelpInfos();
                    mHandler.sendEmptyMessage(MSG_REQUEST_DATA_SUCCESS);

                }

                @Override
                public void onFailed(String errorMsg) {
                    Message msg = Message.obtain();
                    msg.what = MSG_REQUEST_DATA_FAILED;
                    msg.obj = errorMsg;
                    mHandler.sendMessage(msg);
                }
            });
        }

        mHandler = new Handler(mContext.getMainLooper()) {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_REQUEST_DATA_SUCCESS:
                        mLoadingView.setVisibility(View.GONE);
                        mAdapter.notifyDataSetChanged();
                        break;

                    case MSG_REQUEST_DATA_FAILED:
                        mLoadingView.setText(msg.obj != null ? (String) msg.obj : "网络请求失败");
                        break;

                    default:
                        break;
                }
            };
        };
    }

    public CouponHelpDialog(Activity activity) {
        this(activity, 0);
    }

    private void initView() {
        int layoutId = Rx.layout.kg_coupon_help_dialog;
        setContentView(layoutId);

        mLoadingView = (LoadingView) findViewById(Rx.id.kg_loadingView);

        mListView = (ListView) findViewById(Rx.id.kg_lv_select_option);
        if (mListView == null) {
            throw new RuntimeException(
                    "Your content must have a ImageButton whose id attribute is "
                            + "'R.id.kg_all_user_list'");
        }

        mAdapter = new CouponHelpAdapter();
        mListView.setAdapter(mAdapter);
        ((TextView) findViewById(Rx.id.kg_tv_dialog_title)).setText("代金券使用说明");
    }

    public void setDialogDefaultSize() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int parentWidth = dm.widthPixels;
        int parentHeight = dm.heightPixels;

        lp.height = (int) (250 * dm.density);

        if (parentWidth < parentHeight) {// 竖屏
            lp.width = (int) (parentWidth * 0.9);
        } else {
            lp.width = (int) (300 * dm.density);
        }
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.CENTER);
    }

    public void notifyAdapterChage() {
        mAdapter.notifyDataSetInvalidated();
    }

    public void addDatas(ArrayList<CouponHelpInfo> options) {
        if (options != null && options.size() > 0) {
            mOptions.addAll(options);
        }
    }

    public void clearDatas() {
        if (mOptions != null) {
            mOptions.clear();
        }
    }

    class CouponHelpAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (mOptions != null) {
                return mOptions.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return mOptions.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(Rx.layout.kg_coupon_help_item, null);
            }

            TextView title = (TextView) convertView.findViewById(Rx.id.kg_tv_help_title);
            title.setText(mOptions.get(position).getTitle());

            TextView answer = (TextView) convertView.findViewById(Rx.id.kg_tv_help_answer);
            answer.setText(mOptions.get(position).getAnswer());

            return convertView;
        }
    }

}
